# -*- coding: utf-8 -*-
"""
@author: dengpanxiao(@126.com)

@file: distance.py

@time: 17/8/26 下午7:40

@desc: 各种距离度量

"""
# TODO:如果输入为numpy.array时，使用numpy方法求解
import math
import normalization


# 欧式距离
def euclidean_dis(x, y):
    if not x or not y or len(x) != len(y):
        raise Exception("parameter error!")
    res = 0
    for i in range(len(x)):
        res += (x[i]-y[i]) ** 2
    return math.sqrt(res)


# 曼哈顿距离
def manhattan_dis(x, y):
    if not x or not y or len(x) != len(y):
        raise Exception("parameter error!")
    res = 0
    for i in range(len(x)):
        res += abs(x - y)
    return res


# 切比雪夫距离
def chebyshev_dis(x, y):
    if not x or not y or len(x) != len(y):
        raise Exception("parameter error!")
    res = 0
    for i in range(len(x)):
        if res < abs(x - y):
            res = abs(x -y)
    return res


# 闵科夫斯基距离
def minkowski_dis(x, y, p=2):
    if not x or not y or len(x) != len(y) or p <= 0:
        raise Exception("parameter error!")
    if p == 2:
        return euclidean_dis(x, y)
    if p == 1:
        return manhattan_dis(x, y)
    res = 0
    for i in range(len(x)):
        res += (x[i]-y[i]) ** p
    return math.pow(x, 1.0/p)


# 标准化欧式距离
def std_euclidean_dis(x, y):
    if not x or not y or len(x) != len(y):
        raise Exception("parameter error!")
    x = normalization.std_normalize(x)
    y = normalization.std_normalize(y)
    res = 0
    for i in range(len(x)):
        res += (x[i]-y[i]) ** 2
    return math.sqrt(res)

# TODO 马氏距离


